package com.sh.data.engine.domain.api.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sh.data.engine.common.exception.BusinessException;
import com.sh.data.engine.domain.api.model.domain.DataApiDomain;
import com.sh.data.engine.domain.api.model.domain.DataApiMyApplyDomain;
import com.sh.data.engine.domain.api.model.param.DataApiMyApplyParam;
import com.sh.data.engine.domain.api.service.DataApiAuthService;
import com.sh.data.engine.domain.api.service.DataApiMyApplyService;
import com.sh.data.engine.domain.api.service.DataApiService;
import com.sh.data.engine.domain.util.ConvertUtil;
import com.sh.data.engine.repository.dao.api.entity.DataApiEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author : fuchen
 * @date : 2022/1/20 17:46
 */
@Service
public class DataApiMyApplyServiceImpl implements DataApiMyApplyService {

    @Autowired
    private DataApiService dataApiService;

    @Autowired
    private DataApiAuthService dataApiAuthService;

    @Override
    public IPage<DataApiMyApplyDomain> getMyApplyApiDomainPage(DataApiMyApplyParam myApplyParam) {
        if (Objects.isNull(myApplyParam)) {
            throw new BusinessException("参数为空");
        }
        IPage<DataApiMyApplyDomain> dataApiMyApplyEntityPageResult =
            dataApiAuthService.getMyApplyApiPage(
                myApplyParam.getApproveStatus(),
                myApplyParam.getUserId(),
                myApplyParam.getProjectId(),
                myApplyParam.getPageNum(),
                myApplyParam.getPageSize(),
                myApplyParam.getApiName());

        List<DataApiMyApplyDomain> result = dataApiMyApplyEntityPageResult.getRecords();

        List<Long> apiIdList =
            result.stream().map(DataApiMyApplyDomain::getApiId).collect(Collectors.toList());

        if (CollectionUtils.isNotEmpty(apiIdList)) {
            List<DataApiEntity> dataApiEntities = dataApiService.listByIds(apiIdList);
            List<DataApiDomain> dataApiDomains =
                ConvertUtil.copyProperties(dataApiEntities, DataApiDomain.class);

            final Map<Long, DataApiDomain> apiIdMap =
                dataApiDomains.stream()
                    .collect(Collectors.toMap(DataApiDomain::getId, Function.identity()));

            List<DataApiMyApplyDomain> apiDomainPageRecords =
                result.stream()
                    .filter(Objects::nonNull)
                    .map(
                        e -> {
                            DataApiDomain dataApiDomain = apiIdMap.get(e.getApiId());
                            if (null != dataApiDomain) {
                                e.setApiType(dataApiDomain.getApiType());
                                e.setTotalCount(dataApiDomain.getTotalCount());
                                e.setApiDesc(dataApiDomain.getApiDesc());
                            } else {
                                // api记录被物理删除了, 变成脏数据
                                e.setApiType(0);
                                e.setTotalCount(0L);
                                e.setApiDesc("api已删除, 请勿操作");
                            }
                            return e;
                        })
                    .collect(Collectors.toList());
            dataApiMyApplyEntityPageResult.setRecords(apiDomainPageRecords);
        }

        return dataApiMyApplyEntityPageResult;
    }
}
